package org.oy.sealogger.ui;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import org.oy.sealogger.common.Message;
import org.oy.sealogger.common.MessageQueue;

public class InfoBox implements Observer {
	
	private int x = 0;
	private int y = 0;
	private int width = 0;
	private int height = 0;
	
	private Image buttons;
	private ImageLoader imageLoader = new ImageLoader();
	private LoggerGraphics lg = null;
	private Vector<Message> messages = new Vector<Message>();
	
	public InfoBox(LoggerGraphics l) {
		this.lg = l;
		buttons = imageLoader.getImage("INFO.png");
	}

	public void draw(Graphics g, int x, int y, int w, int h) {
		Graphics2D g2 = (Graphics2D)g;
		
		
		this.setVariables(x,y,w,h);
		
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
		g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
		g2.drawImage(buttons, x, y, w, h, this.imageLoader);
		
		setNewMessages(g2, messages);		
	}
	
	private void setNewMessages(Graphics g, Vector<Message> messages) {
		Graphics2D g2 = (Graphics2D)g;
		
		g2.setPaint(Color.BLACK);
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2.setFont(new Font("Arial", Font.ITALIC, 12));
        
        for(int i = 0; i < messages.size(); ++i) {
			String message = messages.get(i).getFormattedMessage();
			//FIXME - tarkoitus olisi vain saada viestit kolmelle riville
			//        info-boxiin. Tämä on vain hätäinen yritys tehdä se, ja
			//        skaalaushan tästä puuttuu kokonaan.
			g2.drawString(message, x + 15, y + 40 + (i * 15));
		}
        
	}
	
	private void setVariables(int x, int y, int w, int h) {
		this.x = x;
		this.y = y;
		this.width = w;
		this.height = h;
	}
	
	public void update(Observable o, Object arg) {
		if(messages.size() == 3)
			messages.remove(messages.size() - 1);
		
		messages.add(0, ((MessageQueue)o).consumeMessage());
		System.out.println(messages.get(0));
        //this.lg.repaint(x - 5, y - 5, x + width + 5, y + height + 5);
		this.lg.repaint();
	}
	
}


